package cn.majingjing.demo.aop.config;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @Author JingjingMa
 * @Date 2019/9/15 12:47
 */
@Component
@Aspect
public class LogAop {
    Logger log = LoggerFactory.getLogger(LogAop.class);

    /**
     * 定义AOP扫描路径
     * 第一个注解只扫描aopTest方法
     */
    @Pointcut(value = "@annotation(cn.majingjing.demo.aop.config.LogToken)")
    public void log() {
        log.info("---LogAop-log()---");
    }

    @Around("log()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        Method method = signature.getMethod();
        String className = method.getDeclaringClass().getName();
        String methodName = method.getName();

        log.info(className+" "+methodName+"\tstart");
        Object proceed;
        proceed = joinPoint.proceed();

        log.info(className+" "+methodName+"\tover");
        return proceed;
    }


}
